home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / os2 / monte.zip / PIPES / HOST.C < prev    next >
Text File  |  1993-03-05  |  2KB  |  76 lines

  1. /* host.c
  2.  
  3. This process hosts a named pipe.  It is one of two closely-cooperating
  4. processes: host.exe and client.exe.
  5.  
  6. The program creates a named pipe, then blocks in DosConnectNPipe until the
  7. client process does an open.
  8.  
  9. Upon connect, the host process reads the pipe.  The data is assumed to be
  10. string data.  The host reverses the characters in the string and write the
  11. result back down the pipe.
  12.  
  13. The host then does a DosDisconnectNPipe, loops, and blocks again in the
  14. connect.
  15.  
  16. Control-break to terminate.
  17.  
  18. */
  19.  
  20. #define INCL_BASE
  21. #include <os2.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <malloc.h>
  26. #include <assert.h>
  27.  
  28. #define LEN_PIPE                      512
  29. #define PIPEMODE_UNIQUE            0x0001
  30. #define OPENMODE_DUPLEX            0X0002
  31.  
  32. int main( int argc, char **argv )
  33. {
  34.   APIRET         rc;
  35.   HFILE          hPipe;
  36.   ULONG          bytes;
  37.   ULONG          bytesread;
  38.   char           achBuffer[ LEN_PIPE ];
  39.  
  40.   // argument is the pipe name to host
  41.   if( argc != 2 ) {
  42.     printf( "supply pipe name on command line like \\PIPE\\FRED\n" );
  43.     return 1;
  44.   }
  45.  
  46.   // create a named pipe in the disconnected state
  47.   rc = DosCreateNPipe( argv[1], &hPipe, OPENMODE_DUPLEX, PIPEMODE_UNIQUE,
  48.     LEN_PIPE, LEN_PIPE, 1000L );
  49.   printf( "DosCreateNPipe rc %d\n", rc );
  50.   assert( rc == 0 );
  51.  
  52.   while( TRUE ) {
  53.  
  54.     // pipe connection occurs when client process does a file open on this pipe
  55.     printf( "waiting for connection\n" );
  56.     rc = DosConnectNPipe( hPipe );
  57.     assert( rc == 0 );
  58.     printf( "connected\n" );
  59.  
  60.     // read data from duplex pipe as client process writes it
  61.     rc = DosRead( hPipe, achBuffer, LEN_PIPE, &bytesread );
  62.     assert( rc == 0 );
  63.  
  64.     // reverse string and write back into pipe as client process reads it
  65.     achBuffer[ bytesread ] = 0;
  66.     strrev( achBuffer );
  67.     rc = DosWrite( hPipe, achBuffer, bytesread, &bytes );
  68.     assert( rc == 0 );
  69.  
  70.     // disconnect long pipe and loop
  71.     DosDisConnectNPipe( hPipe );
  72.     printf( "disconnected\n" );
  73.   }
  74.   return 0;
  75. }
  76.